#!/usr/bin/env python
# coding: utf-8

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

import module.user_manage
import os
    
class MainHandler(webapp.RequestHandler):
    def get(self):
        loginid = self.request.get('loginid')
        email = self.request.get('email')
        deposit = self.request.get('deposit')
        action = self.request.get('action')
        start = self.request.get('start')           # not required, default 0
        limit = self.request.get('limit')           # not required, default 10

        ret = 0            
        
        try:
            start = int(start)
        except:
            start = 0
        try:
            limit = int(limit)
        except:
            limit = 10
        
        try:
            deposit = int(deposit)
            if deposit < 0:
                deposit = 0
        except:
            deposit = 0
        
        self.response.headers['Content-Type'] = 'text/xml'
        if action == "getlist":
            users = module.user_manage.UserManager.get_user_list(start, limit)
            path = os.path.join(os.path.dirname(__file__), '../templetes/user_list.xml')
            self.response.out.write(template.render(path, 
                                                {"users": users}))
            return 
        elif action == "getentry":
            user = module.user_manage.UserManager.get_user_entry(loginid)
            path = os.path.join(os.path.dirname(__file__), '../templetes/user_entry.xml')
            self.response.out.write(template.render(path, 
                                                {"user": user}))
            return 
        elif action == "add":
            ret = module.user_manage.UserManager.add_new_user(loginid, email, deposit)
        elif action == "del":
            ret = module.user_manage.UserManager.del_user(loginid)
        elif action == "modify":
            ret = module.user_manage.UserManager.modify_user_info(loginid, email)
        elif action == "adddepos":
            ret = module.user_manage.UserManager.add_deposit(loginid, deposit)
        elif action == "whtidrawdepos":
            ret = module.user_manage.UserManager.withdraw_deposit(loginid, deposit)
        else:
            ret = -5
            
        path = os.path.join(os.path.dirname(__file__), '../templetes/user_result.xml')
        self.response.out.write(template.render(path, 
                                                {"action": action, 
                                                 "result": ret, 
                                                 "loginid": loginid, 
                                                 "email": email, 
                                                 "deposit": deposit}))

def main():
    application = webapp.WSGIApplication([('/usermanager', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()